/**
 * 
 */
package com.zuipin.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuipin.entity.WinningRecord;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.mapper.WinningRecordMapper;
import com.zuipin.service.IWinningRecordService;
import com.zuipin.util.ConstantsUtil;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.WinningRecordVo;

/** 
 * @ClassName: WinningRecordServiceImpl 
 * @author wuyicheng
 * @time 2017年5月5日上午11:49:15 
 */
@Service
public class WinningRecordServiceImpl implements IWinningRecordService {
	
	@Resource
	private WinningRecordMapper winningRecordMapper;
	
	/** 
	 *
	 * @Title: findById 
	 * @author: wuyicheng
	 * @date: 2017年5月5日下午12:00:38 
	 * @param id
	 * @return
	 * @throws 
	 */
	@Override
	public WinningRecord findById(Long id) {
		return winningRecordMapper.findById(id);
	}
	
	/** 
	 *
	 * @Title: deleteByPrimaryKey 
	 * @author: wuyicheng
	 * @date: 2017年5月5日下午12:00:38 
	 * @param id
	 * @return
	 * @throws 
	 */
	@Override
	public int deleteByPrimaryKey(Long id) {
		WinningRecord winningRecord = new WinningRecord();
		winningRecord.setId(id);
		winningRecord.setDelFlag(ConstantsUtil.DEL_FLAG_YES_1);
		return winningRecordMapper.update(winningRecord);
	}
	
	/** 
	 *
	 * @Title: insert 
	 * @author: wuyicheng
	 * @date: 2017年5月5日下午12:00:38 
	 * @param winningRecord
	 * @return
	 * @throws 
	 */
	@Override
	public int insert(WinningRecord winningRecord) {
		winningRecord.setCreateTime(DateUtils.getCurrentDateTime());
		winningRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		winningRecord.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		return winningRecordMapper.insert(winningRecord);
	}
	
	/** 
	 *
	 * @Title: update 
	 * @author: wuyicheng
	 * @date: 2017年5月5日下午12:00:38 
	 * @param winningRecord
	 * @return
	 * @throws 
	 */
	@Override
	public int update(WinningRecord winningRecord) {
		winningRecord.setUpdateTime(DateUtils.getCurrentDateTime());
		return winningRecordMapper.update(winningRecord);
	}
	
	/** 
	 *
	 * @Title: countUseNumber 
	 * @author: wuyicheng
	 * @date: 2017年5月5日下午5:29:59 
	 * @param memberId
	 * @param sysId
	 * @return
	 * @throws 
	 */
	@Override
	public long countUseNumber(Long memberId, Long sysId) {
		WinningRecord winningRecord = new WinningRecord();
		winningRecord.setMemberId(memberId);
		winningRecord.setSysId(sysId);
		winningRecord.setCreateTime(DateUtils.getCurrentDate());
		return winningRecordMapper.countUseNumber(winningRecord);
	}
	
	/** 
	 *
	 * @Title: searchByPage 
	 * @author: wuyicheng
	 * @date: 2017年5月9日下午2:02:09 
	 * @param pagination
	 * @return
	 * @throws 
	 */
	@Override
	public List<WinningRecord> searchByPage(Pagination pagination) {
		return winningRecordMapper.searchByPage(pagination);
	}
	
	/** 
	 *分页查询中奖记录
	 * @Title: searchByPage 
	 * @author: wuyicheng
	 * @date: 2017年5月9日下午2:02:09 
	 * @param pagination
	 * @return
	 * @throws 
	 */
	@Override
	public PageResult<List<WinningRecordVo>> seachWinningRecordPage(WinningRecord wr,
			Integer pageSize, Integer pageNumber)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		wr.setDelFlag(ConstantsUtil.DEL_FLAG_NO_0);
		wr.setIsThanksJoin(WinningRecord.THANKS_NO);
		PageHelper.startPage(pageNumber, pageSize);
		List<WinningRecord> wrList = winningRecordMapper.seachWinningRecordPage(wr);
		
		PageInfo<WinningRecord> page = new PageInfo<>(wrList);
		PageResult<List<WinningRecordVo>> pageObjectVo = new PageResult<>();
		List<WinningRecordVo> volist = new ArrayList<>();
		for (WinningRecord temp : wrList) {
			WinningRecordVo vo = new WinningRecordVo();
			PropertyUtils.copyProperties(vo, temp);
			volist.add(vo);
		}
		pageObjectVo.setData(volist);
		pageObjectVo.setTotal_record(Integer.valueOf(page.getTotal() + ""));
		pageObjectVo.setErrorCode(ErrorCode.SUCCESS);
		return pageObjectVo;
	}
	
}
